import java.util.concurrent.TimeUnit;

/**
 * @projectName: java-daily-questions
 * @package: PACKAGE_NAME
 * @className: Consumer
 * @author: ZM
 * @description: 消费者
 * @date: 2021/8/21 18:20
 * @version: 1.0
 */
public class Consumer implements Runnable {

    private final Account account;

    public Consumer(Account account) {
        this.account = account;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            synchronized (account) {
                if(account.getBalance() == 0) {
                    try {
                        account.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                account.expenditure(100);
                System.out.println(Thread.currentThread().getName() +
                        "的账户转出100元，账户余额：" +
                        account.getBalance());
                try {
                    TimeUnit.MILLISECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
